home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / gfx / misc / pchglib12.lha / PCHGLib.c < prev    next >
C/C++ Source or Header  |  1992-11-15  |  3KB  |  111 lines

  1. #include <exec/types.h>
  2. #include <iff/pchg.h>
  3. #include <clib/pchglib_protos.h>
  4.  
  5. /****** pchg.lib/--background-- ******************************************
  6.  
  7.    The purpose of this library is to give programmers an easy way to
  8.    implement PCHG display technology in their programs. The library
  9.    allows to read and write easily PCHG chunks by providing
  10.    compression/decompression routines and CopperList building
  11.    functions.
  12.  
  13.    When reading PCHG chunks, you have three function you can use:
  14.    PCHG_ParsePCHG() will automagically parse the chunk (possibly using
  15.    decompression) and build the Copperlist for your ViewPort. If you're
  16.    using the ViewPort of an Intuition Screen, when you will call
  17.    CloseScreen() the Copperlist will be automatically deallocated.
  18.  
  19.    Otherwise, you can use PCHG_SetUserCopList(), which gives you a finer
  20.    control (for instance, you can build a Copperlist with an offset).
  21.    However, the chunk parsing is left to you, apart from the
  22.    decompression, which is handled by PCHG_FastDecomp(). This routine
  23.    is called with register parameter passing, so if you're not using
  24.    SAS C or Assembly, you'd better call PCHG_CFastDecomp(), which has
  25.    standard parameter passing.
  26.  
  27.    When writing a PCHG chunk, the function PCHG_CompHuffmann() will
  28.    pack with static Huffmann encoding your LINEDATA (i.e., the line mask
  29.    followed by a SmallLineChanges or BigLineChanges array, as from
  30.    the PCHG specification).
  31.  
  32.    Finally, the function PCHG_SHAM2PCHG() will build a fake PCHG chunk from
  33.    a SHAM chunk, making easy for people to partially support the old
  34.    "standard".
  35.  
  36.    There are two versions of this library: pchg.lib has stack parameter
  37.    passing, while pchgr.lib was compiled with the SAS/C -rr (registerized
  38.    parameter passing) option.
  39.  
  40.    The prototypes for pchg.lib can be found in clib/pchglib_protos.h, while
  41.    the general PCHG include file is iff/pchg.h.
  42.  
  43.    This library was written by Sebastiano Vigna, and it's placed in
  44.    the public domain.
  45.  
  46. *************************************************************************/
  47.  
  48.  
  49. #if INCLUDE_VERSION<36
  50. FAILURE!! Amiga includes version<36
  51. #endif
  52.  
  53. /* The following structures are used by pchg.lib, and shouldn't be used by
  54. any other program. */
  55.  
  56. struct TreeInternalNode {
  57.     struct TreeNode *Left;
  58.     struct TreeNode *Right;
  59. };
  60.  
  61. struct TreeExternalNode {
  62.     ULONG CodeLength;
  63.     ULONG Code;
  64. };
  65.  
  66. struct TreeNode {
  67.     struct TreeNode *Parent;
  68.     UWORD IsExternal;
  69.     UWORD IsRight;
  70.     union {
  71.         struct TreeInternalNode Int;
  72.         struct TreeExternalNode Ext;
  73.     } n;
  74. };
  75.  
  76. /* This routine is here for historical interest. Is a C version of
  77. PCHG_FastDecomp(). */
  78.  
  79. VOID PCHG_Decompress(ULONG *Source, UBYTE *Dest, WORD *Tree, ULONG OriginalSize) {
  80.     ULONG i = 0, bits = 0;
  81.     ULONG CurLongword;
  82.     SHORT *p = Tree;
  83.  
  84.     while(i<OriginalSize) {
  85.         if (!bits) {
  86.             CurLongword = *(Source++);
  87.             bits = 32;
  88.         }
  89.         if (CurLongword & 0x80000000) {
  90.             if (*p>=0) {
  91.                 *(Dest++) = (unsigned char)*p;
  92.                 i++;
  93.                 p = Tree;
  94.             }
  95.             else p+=(*p/2);
  96.         }
  97.         else {
  98.             p--;
  99.             if (*p>0 && (*p & 0x100)) {
  100.                 *(Dest++) = (unsigned char)*p;
  101.                 i++;
  102.                 p = Tree;
  103.             }
  104.         }
  105.         CurLongword <<= 1;
  106.         bits--;
  107.     }
  108. }
  109.  
  110.  
  111.